﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace TweetMemeNET
{
    /// <summary>
    /// Represents a single TweetMeme comment
    /// </summary>
    public class Comment
    {
        /// <summary>
        /// Instantiates a new Comment object based on an XML string
        /// </summary>
        /// <param name="xml"></param>
        public Comment(string xml)
        {
            Populate(XElement.Parse(xml));
        }

        /// <summary>
        /// Instantiates a new Comment object based on an XElement object
        /// </summary>
        /// <param name="tweetElement"></param>
        public Comment(XElement e)
        {
            Populate(e);
        }

        protected void Populate(XElement commentElement)
        {
            // Get comment properties
            ID=commentElement.Element(XName.Get("id", string.Empty)).Value;
            Text=commentElement.Element(XName.Get("text", string.Empty)).Value;
            UrlID=commentElement.Element(XName.Get("url_id", string.Empty)).Value;
            NumberOfRetweets=int.Parse(commentElement.Element(XName.Get("retweets", string.Empty)).Value);
            UserID=commentElement.Element(XName.Get("user_id", string.Empty)).Value;
            LastReferenced=DateTime.Parse(commentElement.Element(XName.Get("last_referenced", string.Empty)).Value);
            Created=DateTime.Parse(ID=commentElement.Element(XName.Get("created_at", string.Empty)).Value);
 
            // Get alias
            XElement aliasElement = commentElement.Element(XName.Get("alias", string.Empty));
            Alias=aliasElement!=null ? aliasElement.Value : string.Empty;
           
            // Get user properties
            XElement user = commentElement.Element(XName.Get("user", string.Empty));
            User = new User(user);

            // Get story properties
            XElement story = commentElement.Element(XName.Get("story", string.Empty));
            Story = new Story(story);
        }

        /// <summary>
        /// Gets the ID of the comment
        /// </summary>
        public string ID { get; protected set; }

        /// <summary>
        /// Gets the URL ID of the comment
        /// </summary>
        public string UrlID { get; protected set; }

        /// <summary>
        /// Gets the date and time of the comment
        /// </summary>
        public DateTime Created { get; protected set; }

        /// <summary>
        /// Gets the last reference date and time of the comment
        /// </summary>
        public DateTime LastReferenced { get; protected set; }

        /// <summary>
        /// Gets the actual tweet text
        /// </summary>
        public string Text { get; protected set; }

        /// <summary>
        /// Gets the ID of the user
        /// </summary>
        public string UserID { get; protected set; }

        /// <summary>
        /// Gets the alias of the user
        /// </summary>
        public string Alias { get; protected set; }

        /// <summary>
        /// Gets the number of retweets for the story
        /// </summary>
        public int NumberOfRetweets { get; protected set; }

        /// <summary>
        /// Gets the user of the comment
        /// </summary>
        public User User { get; protected set; }

        /// <summary>
        /// Gets the associated story of the comment
        /// </summary>
        public Story Story { get; protected set; }
    }
}
